import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Demo2MC {
    static Process process;
    final static BufferedWriter[] bw = {null};
    final static DataOutputStream[] out = {null};
    public void CmdTest() throws IOException {
//        process=Runtime.getRuntime().exec("cmd");
        new Thread(){
            public void run() {
                try {
                    process=Runtime.getRuntime().exec("/bin/sh run.sh");
                    bw[0] =new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
//                    bw[0].write("/bin/sh run.sh");
//                    bw[0].write("java -jar nukkit.jar");
//                    bw[0].newLine();
//                    bw[0].flush();
                    read();
                    Scanner scanner = new Scanner(System.in);

                    while (true) {
                        String str = scanner.nextLine();
                        bw[0].write(str);
                        bw[0].newLine();
                        bw[0].flush();
                    }



                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }.start();


    }

    public static void socket() {
        new Thread() {
            public void run() {
                try {
                    //创建Socket对象
                    ServerSocket ss = new ServerSocket(10086);
                    //监听（阻塞）
                    System.out.println("等待连接");
                    while (true) {
                        Socket s = ss.accept();
                        //获取输入流对象
                        InputStream is = s.getInputStream();
                        //获取数据
                        byte[] bys = new byte[1024];
                        int len;
                        len = is.read(bys);
                        //输出数据
                        String cmd = new String(bys,0,len,"GBK");
                        out[0] = new DataOutputStream(s.getOutputStream());
                        if (cmd.equals("conn")) {
                            System.out.println("QQ机器人已连接");
                            out[0].writeUTF("Hello!");
                        } else if (cmd.equals("Handshake")) {
                            out[0].writeUTF("Handshake");
                        } else if (cmd.equals("重启服务器")) {
                            out[0].writeUTF("正在重启");
                            process.destroy();
//                            out[0].close();
                            new Demo().CmdTest();
                        } else {
                            System.out.println("收到机器人指令： " + cmd);
                            bw[0].write(cmd);
                            bw[0].newLine();
                            bw[0].flush();
                            out[0].writeUTF("Complete!");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public static void read() {
        new Thread() {
            public void run() {
                try {
                    BufferedReader br=new BufferedReader(new InputStreamReader(process.getInputStream()));
                    String cmdout="";
                    while ((cmdout=br.readLine())!=null) {
                        System.out.println(cmdout);
                        if (out[0] != null) {
                            out[0].writeUTF(cmdout);
                            //OS: Linux
                        }
                        if (cmdout.indexOf("started") != -1) {
                            System.out.println("服务器已启动，Demo构建版本：Linux#16");
                            if (out[0] != null) {
                                out[0].writeUTF("服务器已启动");
                                //OS: Linux
                            }
                        }
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    try {
                        out[0].close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }.start();
    }

    public static void main(String[] args) throws IOException {
        Demo2MC cmd=new Demo2MC();
        socket();
        cmd.CmdTest();
    }
